﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Globalization;

namespace Paypayes.Inventory
{
    public partial class AddChangeItemForm : Form
    {
        private String _itemGuid;
        private Int32 _sizeChildrenCount = 0;
        private Int32 _sizeAdultsCount = 0;
        private XElement _currentElement;
        private Boolean _isModifyingItem = false;

        public AddChangeItemForm()
        {
            InitializeComponent();
            _itemGuid = Guid.NewGuid().ToString();
            txtSizeSum.Text = "0";
        }

        public void Initialize(XElement baseElement)
        {
            _itemGuid = baseElement.Attribute("id").Value;
            txtDescription.Text = baseElement.Element("description") == null ? "" : baseElement.Element("description").Value;
            txtBox.Text = baseElement.Element("box") == null ? "" : baseElement.Element("box").Value;
            txtPicturePath.Text = baseElement.Element("picture") == null ? "" : baseElement.Element("picture").Value;
            if (!String.IsNullOrEmpty(txtPicturePath.Text))
            {
                pictureBox1.Load(txtPicturePath.Text);
            }

            XElement sizeElement = baseElement.Element("sizes");

            if (sizeElement != null)
            {
                XElement subSizeElement = sizeElement.Element("children");
                if (subSizeElement != null)
                {
                    txtSizeAge4.Text = subSizeElement.Element("s4yo") == null ? "0" : subSizeElement.Element("s4yo").Value;
                    txtSizeAge6.Text = subSizeElement.Element("s6yo") == null ? "0" : subSizeElement.Element("s6yo").Value;
                    txtSizeAge8.Text = subSizeElement.Element("s8yo") == null ? "0" : subSizeElement.Element("s8yo").Value;
                    txtSizeAge10.Text = subSizeElement.Element("s10yo") == null ? "0" : subSizeElement.Element("s10yo").Value;
                    txtSizeAge12.Text = subSizeElement.Element("s12yo") == null ? "0" : subSizeElement.Element("s12yo").Value;
                    txtSizeAge14.Text = subSizeElement.Element("s14yo") == null ? "0" : subSizeElement.Element("s14yo").Value;
                }

                subSizeElement = sizeElement.Element("adults");
                if (subSizeElement != null)
                {
                    txtSize36.Text = subSizeElement.Element("s36") == null ? "0" : subSizeElement.Element("s36").Value;
                    txtSize38.Text = subSizeElement.Element("s38") == null ? "0" : subSizeElement.Element("s38").Value;
                    txtSize40.Text = subSizeElement.Element("s40") == null ? "0" : subSizeElement.Element("s40").Value;
                    txtSize42.Text = subSizeElement.Element("s42") == null ? "0" : subSizeElement.Element("s42").Value;
                    txtSize44.Text = subSizeElement.Element("s44") == null ? "0" : subSizeElement.Element("s44").Value;
                    txtSizeOther.Text = subSizeElement.Element("other") == null ? "0" : subSizeElement.Element("other").Value;
                }
            }

            _currentElement = baseElement;
            _isModifyingItem = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            XElement _xEle = MainForm.XElem;

            XElement itemElement = new XElement("item",
                new XAttribute("id", _itemGuid),
                new XElement("description", txtDescription.Text),
                new XElement("box", txtBox.Text),
                new XElement("picture", txtPicturePath.Text)
                );

            if (txtSizeSum.Text != "0")
            {
                XElement sizesElement = new XElement("sizes");
                if (_sizeChildrenCount != 0)
                {
                    XElement childrenElement = new XElement("children");

                    if (txtSizeAge4.Text != "0")
                    {
                        childrenElement.Add(new XElement("s4yo", txtSizeAge4.Text));
                    }
                    if (txtSizeAge6.Text != "0")
                    {
                        childrenElement.Add(new XElement("s6yo", txtSizeAge6.Text));
                    }
                    if (txtSizeAge8.Text != "0")
                    {
                        childrenElement.Add(new XElement("s8yo", txtSizeAge8.Text));
                    }
                    if (txtSizeAge10.Text != "0")
                    {
                        childrenElement.Add(new XElement("s10yo", txtSizeAge10.Text));
                    }
                    if (txtSizeAge12.Text != "0")
                    {
                        childrenElement.Add(new XElement("s12yo", txtSizeAge12.Text));
                    }
                    if (txtSizeAge14.Text != "0")
                    {
                        childrenElement.Add(new XElement("s14yo", txtSizeAge14.Text));
                    }

                    childrenElement.Add(new XAttribute("total", _sizeChildrenCount));
                    sizesElement.Add(childrenElement);
                }

                if (_sizeAdultsCount != 0)
                {
                    XElement adultsElement = new XElement("adults");

                    foreach (Control control in groupBox3.Controls)
                    {
                        if (control is TextBox)
                        {
                            TextBox textBox = control as TextBox;
                            if (textBox.Text != "0")
                            {
                                adultsElement.Add(new XElement(textBox.Tag as String, textBox.Text));
                            }
                        }
                    }
                    adultsElement.Add(new XAttribute("total", _sizeAdultsCount));
                    sizesElement.Add(adultsElement);
                }

                sizesElement.Add(new XAttribute("total", txtSizeSum.Text));

                itemElement.Add(sizesElement);
            }

            if (_isModifyingItem)
            {
                XElement result = _xEle.Elements("item")
                    .Single(x => (string)x.Attribute("id") == itemElement.Attribute("id").Value);
                result.ReplaceWith(itemElement);
                Image image = null;
                try
                {
                    image = Image.FromFile(txtPicturePath.Text);
                }
                catch (Exception)
                {
                    image = Image.FromFile(@"vide.jpg");
                }
                MainForm.thumbnails[_itemGuid] = image.GetThumbnailImage(120, 120, () => false, IntPtr.Zero);
            }
            else
            {
                    Image image = null;
                    try
                    {
                        image = Image.FromFile(txtPicturePath.Text);
                    }
                    catch (Exception)
                    {
                        image = Image.FromFile(@"vide.jpg");
                    }
                    MainForm.thumbnails.Add(_itemGuid, image.GetThumbnailImage(120, 120, () => false, IntPtr.Zero));
                _xEle.Add(itemElement);
            }

            this.Close();
        }

        private void txtSizeChanged(object sender, EventArgs e)
        {
            _sizeChildrenCount = 0;
            foreach (Control control in groupBox2.Controls)
            {
                if (control is TextBox)
                {
                    _sizeChildrenCount += String.IsNullOrEmpty(control.Text) ? 0 : Convert.ToInt32(control.Text);
                }
            }

            _sizeAdultsCount = 0;
            foreach (Control control in groupBox3.Controls)
            {
                if (control is TextBox)
                {
                    _sizeAdultsCount += String.IsNullOrEmpty(control.Text) ? 0 : Convert.ToInt32(control.Text);
                }
            }

            txtSizeSum.Text = Convert.ToString(_sizeChildrenCount + _sizeAdultsCount);
        }

        private void btnPicture_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                pictureBox1.Load(openFileDialog1.FileName);
                txtPicturePath.Text = openFileDialog1.FileName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TextBoxes_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;

            string keyInput = e.KeyChar.ToString();

            if (Char.IsDigit(e.KeyChar))
            {
                // Digits are OK
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
             keyInput.Equals(negativeSign))
            {
                // Decimal separator is OK
            }
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            else
            {
                e.Handled = true;
                //    MessageBeep();
            } 
        }
    }
}
